﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
using Commerce.PayPal;
using umbraco.cms.businesslogic.media;


namespace CommerceForUmbraco.usercontrols
{
    public partial class HeaderCart : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CountCartItems();
        }

        protected override void OnPreRender(EventArgs e)
        {
            CountCartItems();
        }

        protected void btnCart_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utility.GetCartUrl(), true);
        }

        protected void btnCheckout_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utility.GetCheckoutUrl(), true);

        }

        private void CountCartItems()
        {
            OrderItemCollection orderItemCollection = OrderController.GetCartItems();

            int totalItems = 0;
            foreach (var prod in orderItemCollection)
            {
                totalItems += prod.Quantity;
            }

            if (totalItems > 0)
            {
                if (totalItems == 1)
                {
                    btnCart.Text = "<span>1</span> item";
                }
                else
                {
                    btnCart.Text = String.Format("<span>{0}</span> items", totalItems);
                }
            }
        }

    }
}